package com.media.ffmpeg;

import android.util.Log;

import java.lang.ref.WeakReference;

public class FFMpegPlayer {

    private static final String TAG = "kkunion";

    private static final int MEDIA_NOP = 0; // interface test message
    private static final int MEDIA_PREPARED = 1;
    private static final int MEDIA_PLAYBACK_COMPLETE = 2;
    private static final int MEDIA_BUFFERING_UPDATE = 3;
    private static final int MEDIA_SEEK_COMPLETE = 4;
    private static final int MEDIA_SET_VIDEO_SIZE = 5;
    private static final int MEDIA_ERROR = 100;
    private static final int MEDIA_INFO = 200;


    public interface OnPlayerEventListener {
        void onVideoSizeChanged(int rotate, int width, int height);
        void onVideoFrameUpdated();
    }

    private OnPlayerEventListener onPlayerEventListener;

    static {
        System.loadLibrary("avcodec-57");
        System.loadLibrary("avdevice-57");
        System.loadLibrary("avfilter-6");
        System.loadLibrary("avformat-57");
        System.loadLibrary("avutil-55");
        System.loadLibrary("postproc-54");
        System.loadLibrary("swresample-2");
        System.loadLibrary("swscale-4");

        System.loadLibrary("media_jni");
    }

    private int mNativeContext; // accessed by native methods


    public FFMpegPlayer() {
        _init(new WeakReference<FFMpegPlayer>(this));
    }

    public void setOnPlayerEventListener(OnPlayerEventListener listener) {
        this.onPlayerEventListener = listener;
    }

    /**
     * Called from native code when an interesting event happens.  This method
     * just uses the EventHandler system to post the event back to the main app thread.
     * We use a weak reference to the original MediaPlayer object so that the native
     * code is safe from the object disappearing from underneath it.  (This is
     * the cookie passed to native_setup().)
     */
    private void postEventFromNative(Object mediaplayer_ref,
                                            int what, int arg1, int arg2, int arg3, Object obj)
    {
        Log.d(TAG, "postEventFromNative----------------- what:"+what+ " arg1:"+arg1+ " arg2:"+arg2+" arg3:"+arg3);

        switch(what) {
            case MEDIA_PREPARED:
                int rotate = arg1;
                int width = 0;
                int height = 0;

                if (rotate == 0) {// (横屏拍摄)正常方向
                    width = arg2;
                    height = arg3;

                } else if (rotate == 90) { //(竖屏拍摄)视频旋转了90度，宽高需要交换
                    width = arg3;
                    height = arg2;

                } else if (rotate == 180) {//(横屏拍摄)非正常方向
                    width = arg2;
                    height = arg3;

                } else if (rotate == 270) {//(竖屏拍摄)视频旋转了270度，宽高需要交换
                    width = arg3;
                    height = arg2;
                }
                onPlayerEventListener.onVideoSizeChanged(rotate, width, height);
                break;

            default:
                break;
        }
    }

    public void postEventVideoFrameUpdated() {
        onPlayerEventListener.onVideoFrameUpdated();
    }

    private native void _init(Object thisPlayer) throws RuntimeException;
    public native void _setDataSource(String path);
    public native void _prepare();
    public native void _start();
    public native void _seekTo(long msec);
    public native void _stop();
    public native boolean _isPlaying();
    public native int _getVideoWidth();
    public native int _getVideoHeight();
    public native long _getCurrentPosition();
    public native long _getDuration();


    public native void _initViewPort(int rotate, int width, int height);

    public native void _step();
}
